﻿using Pay.Core;
using Pay.Data;
using Pay.DTO;
using Pay.Service.Cache;
using Pay.IService;
using System;
using System.Linq;

namespace Pay.Service {
    public class IPFilterService : ServiceBase, IIPFilterService, IDisposable {
        private IUnitOfWork _iUnitOfWork;
        public IPFilterService(IUnitOfWork iUnitOfWork)
            : base(iUnitOfWork) {
            _iUnitOfWork = iUnitOfWork;
        }
        public bool AddIp(string ip, bool isWhiteIp) {
            bool exist = context.IPFilterInfo.Exist(s => s.Ip == ip);
            if (!exist) {
                IPFilterInfo model = new IPFilterInfo();
                model.Ip = ip;
                model.IntIp = IPHelper.IpToInt(ip);
                model.CreateTime = DateTime.Now.ToUnix();
                model.IsWihteIp = isWhiteIp;
                context.IPFilterInfo.Add(model);
                return _iUnitOfWork.Commit("IPFilterInfo") > 0;
            } else return false;

        }
        public ip_filter_dto GetIP(string ip) {
            var model = context.IPFilterInfo.Where(s => s.Ip == ip).FromCacheFirstOrDefault<IPFilterInfo, ip_filter_dto>("IPFilterInfo");
            return model;
        }
    }
}
